{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "<h1 style=\"color:blue\">Reshape dataframe using stack/unstack</h1>"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 81,
   "metadata": {
    "collapsed": false,
    "scrolled": true
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style>\n",
       "    .dataframe thead tr:only-child th {\n",
       "        text-align: right;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: left;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr>\n",
       "      <th></th>\n",
       "      <th colspan=\"3\" halign=\"left\">Price</th>\n",
       "      <th colspan=\"3\" halign=\"left\">Price to earnings ratio (P/E)</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Company</th>\n",
       "      <th>Facebook</th>\n",
       "      <th>Google</th>\n",
       "      <th>Microsoft</th>\n",
       "      <th>Facebook</th>\n",
       "      <th>Google</th>\n",
       "      <th>Microsoft</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>2017-06-05</th>\n",
       "      <td>155</td>\n",
       "      <td>955</td>\n",
       "      <td>66</td>\n",
       "      <td>37.10</td>\n",
       "      <td>32.0</td>\n",
       "      <td>30.31</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2017-06-06</th>\n",
       "      <td>150</td>\n",
       "      <td>987</td>\n",
       "      <td>69</td>\n",
       "      <td>36.98</td>\n",
       "      <td>31.3</td>\n",
       "      <td>30.56</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2017-06-07</th>\n",
       "      <td>153</td>\n",
       "      <td>963</td>\n",
       "      <td>62</td>\n",
       "      <td>36.78</td>\n",
       "      <td>31.7</td>\n",
       "      <td>30.46</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2017-06-08</th>\n",
       "      <td>155</td>\n",
       "      <td>1000</td>\n",
       "      <td>61</td>\n",
       "      <td>36.11</td>\n",
       "      <td>31.2</td>\n",
       "      <td>30.11</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2017-06-09</th>\n",
       "      <td>156</td>\n",
       "      <td>1012</td>\n",
       "      <td>66</td>\n",
       "      <td>37.07</td>\n",
       "      <td>30.0</td>\n",
       "      <td>31.00</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "              Price                  Price to earnings ratio (P/E)         \\\n",
       "Company    Facebook Google Microsoft                      Facebook Google   \n",
       "2017-06-05      155    955        66                         37.10   32.0   \n",
       "2017-06-06      150    987        69                         36.98   31.3   \n",
       "2017-06-07      153    963        62                         36.78   31.7   \n",
       "2017-06-08      155   1000        61                         36.11   31.2   \n",
       "2017-06-09      156   1012        66                         37.07   30.0   \n",
       "\n",
       "                      \n",
       "Company    Microsoft  \n",
       "2017-06-05     30.31  \n",
       "2017-06-06     30.56  \n",
       "2017-06-07     30.46  \n",
       "2017-06-08     30.11  \n",
       "2017-06-09     31.00  "
      ]
     },
     "execution_count": 81,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "import pandas as pd\n",
    "df = pd.read_excel(\"stocks.xlsx\",header=[0,1])\n",
    "df"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 89,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style>\n",
       "    .dataframe thead tr:only-child th {\n",
       "        text-align: right;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: left;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th>Price</th>\n",
       "      <th>Price to earnings ratio (P/E)</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th></th>\n",
       "      <th>Company</th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th rowspan=\"3\" valign=\"top\">2017-06-05</th>\n",
       "      <th>Facebook</th>\n",
       "      <td>155</td>\n",
       "      <td>37.10</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Google</th>\n",
       "      <td>955</td>\n",
       "      <td>32.00</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Microsoft</th>\n",
       "      <td>66</td>\n",
       "      <td>30.31</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th rowspan=\"3\" valign=\"top\">2017-06-06</th>\n",
       "      <th>Facebook</th>\n",
       "      <td>150</td>\n",
       "      <td>36.98</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Google</th>\n",
       "      <td>987</td>\n",
       "      <td>31.30</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Microsoft</th>\n",
       "      <td>69</td>\n",
       "      <td>30.56</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th rowspan=\"3\" valign=\"top\">2017-06-07</th>\n",
       "      <th>Facebook</th>\n",
       "      <td>153</td>\n",
       "      <td>36.78</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Google</th>\n",
       "      <td>963</td>\n",
       "      <td>31.70</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Microsoft</th>\n",
       "      <td>62</td>\n",
       "      <td>30.46</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th rowspan=\"3\" valign=\"top\">2017-06-08</th>\n",
       "      <th>Facebook</th>\n",
       "      <td>155</td>\n",
       "      <td>36.11</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Google</th>\n",
       "      <td>1000</td>\n",
       "      <td>31.20</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Microsoft</th>\n",
       "      <td>61</td>\n",
       "      <td>30.11</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th rowspan=\"3\" valign=\"top\">2017-06-09</th>\n",
       "      <th>Facebook</th>\n",
       "      <td>156</td>\n",
       "      <td>37.07</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Google</th>\n",
       "      <td>1012</td>\n",
       "      <td>30.00</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Microsoft</th>\n",
       "      <td>66</td>\n",
       "      <td>31.00</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "                      Price  Price to earnings ratio (P/E)\n",
       "           Company                                        \n",
       "2017-06-05 Facebook     155                          37.10\n",
       "           Google       955                          32.00\n",
       "           Microsoft     66                          30.31\n",
       "2017-06-06 Facebook     150                          36.98\n",
       "           Google       987                          31.30\n",
       "           Microsoft     69                          30.56\n",
       "2017-06-07 Facebook     153                          36.78\n",
       "           Google       963                          31.70\n",
       "           Microsoft     62                          30.46\n",
       "2017-06-08 Facebook     155                          36.11\n",
       "           Google      1000                          31.20\n",
       "           Microsoft     61                          30.11\n",
       "2017-06-09 Facebook     156                          37.07\n",
       "           Google      1012                          30.00\n",
       "           Microsoft     66                          31.00"
      ]
     },
     "execution_count": 89,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df.stack()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 90,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style>\n",
       "    .dataframe thead tr:only-child th {\n",
       "        text-align: right;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: left;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>Company</th>\n",
       "      <th>Facebook</th>\n",
       "      <th>Google</th>\n",
       "      <th>Microsoft</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th rowspan=\"2\" valign=\"top\">2017-06-05</th>\n",
       "      <th>Price</th>\n",
       "      <td>155.00</td>\n",
       "      <td>955.0</td>\n",
       "      <td>66.00</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Price to earnings ratio (P/E)</th>\n",
       "      <td>37.10</td>\n",
       "      <td>32.0</td>\n",
       "      <td>30.31</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th rowspan=\"2\" valign=\"top\">2017-06-06</th>\n",
       "      <th>Price</th>\n",
       "      <td>150.00</td>\n",
       "      <td>987.0</td>\n",
       "      <td>69.00</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Price to earnings ratio (P/E)</th>\n",
       "      <td>36.98</td>\n",
       "      <td>31.3</td>\n",
       "      <td>30.56</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th rowspan=\"2\" valign=\"top\">2017-06-07</th>\n",
       "      <th>Price</th>\n",
       "      <td>153.00</td>\n",
       "      <td>963.0</td>\n",
       "      <td>62.00</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Price to earnings ratio (P/E)</th>\n",
       "      <td>36.78</td>\n",
       "      <td>31.7</td>\n",
       "      <td>30.46</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th rowspan=\"2\" valign=\"top\">2017-06-08</th>\n",
       "      <th>Price</th>\n",
       "      <td>155.00</td>\n",
       "      <td>1000.0</td>\n",
       "      <td>61.00</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Price to earnings ratio (P/E)</th>\n",
       "      <td>36.11</td>\n",
       "      <td>31.2</td>\n",
       "      <td>30.11</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th rowspan=\"2\" valign=\"top\">2017-06-09</th>\n",
       "      <th>Price</th>\n",
       "      <td>156.00</td>\n",
       "      <td>1012.0</td>\n",
       "      <td>66.00</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Price to earnings ratio (P/E)</th>\n",
       "      <td>37.07</td>\n",
       "      <td>30.0</td>\n",
       "      <td>31.00</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "Company                                   Facebook  Google  Microsoft\n",
       "2017-06-05 Price                            155.00   955.0      66.00\n",
       "           Price to earnings ratio (P/E)     37.10    32.0      30.31\n",
       "2017-06-06 Price                            150.00   987.0      69.00\n",
       "           Price to earnings ratio (P/E)     36.98    31.3      30.56\n",
       "2017-06-07 Price                            153.00   963.0      62.00\n",
       "           Price to earnings ratio (P/E)     36.78    31.7      30.46\n",
       "2017-06-08 Price                            155.00  1000.0      61.00\n",
       "           Price to earnings ratio (P/E)     36.11    31.2      30.11\n",
       "2017-06-09 Price                            156.00  1012.0      66.00\n",
       "           Price to earnings ratio (P/E)     37.07    30.0      31.00"
      ]
     },
     "execution_count": 90,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df.stack(level=0)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 92,
   "metadata": {
    "collapsed": false,
    "scrolled": true
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style>\n",
       "    .dataframe thead tr:only-child th {\n",
       "        text-align: right;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: left;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th>Price</th>\n",
       "      <th>Price to earnings ratio (P/E)</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th></th>\n",
       "      <th>Company</th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th rowspan=\"3\" valign=\"top\">2017-06-05</th>\n",
       "      <th>Facebook</th>\n",
       "      <td>155</td>\n",
       "      <td>37.10</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Google</th>\n",
       "      <td>955</td>\n",
       "      <td>32.00</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Microsoft</th>\n",
       "      <td>66</td>\n",
       "      <td>30.31</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th rowspan=\"3\" valign=\"top\">2017-06-06</th>\n",
       "      <th>Facebook</th>\n",
       "      <td>150</td>\n",
       "      <td>36.98</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Google</th>\n",
       "      <td>987</td>\n",
       "      <td>31.30</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Microsoft</th>\n",
       "      <td>69</td>\n",
       "      <td>30.56</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th rowspan=\"3\" valign=\"top\">2017-06-07</th>\n",
       "      <th>Facebook</th>\n",
       "      <td>153</td>\n",
       "      <td>36.78</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Google</th>\n",
       "      <td>963</td>\n",
       "      <td>31.70</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Microsoft</th>\n",
       "      <td>62</td>\n",
       "      <td>30.46</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th rowspan=\"3\" valign=\"top\">2017-06-08</th>\n",
       "      <th>Facebook</th>\n",
       "      <td>155</td>\n",
       "      <td>36.11</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Google</th>\n",
       "      <td>1000</td>\n",
       "      <td>31.20</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Microsoft</th>\n",
       "      <td>61</td>\n",
       "      <td>30.11</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th rowspan=\"3\" valign=\"top\">2017-06-09</th>\n",
       "      <th>Facebook</th>\n",
       "      <td>156</td>\n",
       "      <td>37.07</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Google</th>\n",
       "      <td>1012</td>\n",
       "      <td>30.00</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Microsoft</th>\n",
       "      <td>66</td>\n",
       "      <td>31.00</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "                      Price  Price to earnings ratio (P/E)\n",
       "           Company                                        \n",
       "2017-06-05 Facebook     155                          37.10\n",
       "           Google       955                          32.00\n",
       "           Microsoft     66                          30.31\n",
       "2017-06-06 Facebook     150                          36.98\n",
       "           Google       987                          31.30\n",
       "           Microsoft     69                          30.56\n",
       "2017-06-07 Facebook     153                          36.78\n",
       "           Google       963                          31.70\n",
       "           Microsoft     62                          30.46\n",
       "2017-06-08 Facebook     155                          36.11\n",
       "           Google      1000                          31.20\n",
       "           Microsoft     61                          30.11\n",
       "2017-06-09 Facebook     156                          37.07\n",
       "           Google      1012                          30.00\n",
       "           Microsoft     66                          31.00"
      ]
     },
     "execution_count": 92,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df_stacked=df.stack()\n",
    "df_stacked"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 93,
   "metadata": {
    "collapsed": false,
    "scrolled": true
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style>\n",
       "    .dataframe thead tr:only-child th {\n",
       "        text-align: right;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: left;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr>\n",
       "      <th></th>\n",
       "      <th colspan=\"3\" halign=\"left\">Price</th>\n",
       "      <th colspan=\"3\" halign=\"left\">Price to earnings ratio (P/E)</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Company</th>\n",
       "      <th>Facebook</th>\n",
       "      <th>Google</th>\n",
       "      <th>Microsoft</th>\n",
       "      <th>Facebook</th>\n",
       "      <th>Google</th>\n",
       "      <th>Microsoft</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>2017-06-05</th>\n",
       "      <td>155</td>\n",
       "      <td>955</td>\n",
       "      <td>66</td>\n",
       "      <td>37.10</td>\n",
       "      <td>32.0</td>\n",
       "      <td>30.31</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2017-06-06</th>\n",
       "      <td>150</td>\n",
       "      <td>987</td>\n",
       "      <td>69</td>\n",
       "      <td>36.98</td>\n",
       "      <td>31.3</td>\n",
       "      <td>30.56</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2017-06-07</th>\n",
       "      <td>153</td>\n",
       "      <td>963</td>\n",
       "      <td>62</td>\n",
       "      <td>36.78</td>\n",
       "      <td>31.7</td>\n",
       "      <td>30.46</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2017-06-08</th>\n",
       "      <td>155</td>\n",
       "      <td>1000</td>\n",
       "      <td>61</td>\n",
       "      <td>36.11</td>\n",
       "      <td>31.2</td>\n",
       "      <td>30.11</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2017-06-09</th>\n",
       "      <td>156</td>\n",
       "      <td>1012</td>\n",
       "      <td>66</td>\n",
       "      <td>37.07</td>\n",
       "      <td>30.0</td>\n",
       "      <td>31.00</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "              Price                  Price to earnings ratio (P/E)         \\\n",
       "Company    Facebook Google Microsoft                      Facebook Google   \n",
       "2017-06-05      155    955        66                         37.10   32.0   \n",
       "2017-06-06      150    987        69                         36.98   31.3   \n",
       "2017-06-07      153    963        62                         36.78   31.7   \n",
       "2017-06-08      155   1000        61                         36.11   31.2   \n",
       "2017-06-09      156   1012        66                         37.07   30.0   \n",
       "\n",
       "                      \n",
       "Company    Microsoft  \n",
       "2017-06-05     30.31  \n",
       "2017-06-06     30.56  \n",
       "2017-06-07     30.46  \n",
       "2017-06-08     30.11  \n",
       "2017-06-09     31.00  "
      ]
     },
     "execution_count": 93,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df_stacked.unstack()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "<h1 style=\"color:blue\">3 levels of column headers</h1>"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 95,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style>\n",
       "    .dataframe thead tr:only-child th {\n",
       "        text-align: right;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: left;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr>\n",
       "      <th></th>\n",
       "      <th colspan=\"6\" halign=\"left\">Price Ratios</th>\n",
       "      <th colspan=\"6\" halign=\"left\">Income Statement</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th></th>\n",
       "      <th colspan=\"3\" halign=\"left\">Price</th>\n",
       "      <th colspan=\"3\" halign=\"left\">Price to earnings ratio (P/E)</th>\n",
       "      <th colspan=\"3\" halign=\"left\">Net Sales</th>\n",
       "      <th colspan=\"3\" halign=\"left\">Net Profit</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Company</th>\n",
       "      <th>Facebook</th>\n",
       "      <th>Google</th>\n",
       "      <th>Microsoft</th>\n",
       "      <th>Facebook</th>\n",
       "      <th>Google</th>\n",
       "      <th>Microsoft</th>\n",
       "      <th>Facebook</th>\n",
       "      <th>Google</th>\n",
       "      <th>Microsoft</th>\n",
       "      <th>Facebook</th>\n",
       "      <th>Google</th>\n",
       "      <th>Microsoft</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>Q1 2016</th>\n",
       "      <td>155</td>\n",
       "      <td>955</td>\n",
       "      <td>66</td>\n",
       "      <td>37.10</td>\n",
       "      <td>32.0</td>\n",
       "      <td>30.31</td>\n",
       "      <td>2.6</td>\n",
       "      <td>20</td>\n",
       "      <td>18.70</td>\n",
       "      <td>0.80</td>\n",
       "      <td>5.43</td>\n",
       "      <td>4.56</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Q2 2016</th>\n",
       "      <td>150</td>\n",
       "      <td>987</td>\n",
       "      <td>69</td>\n",
       "      <td>36.98</td>\n",
       "      <td>31.3</td>\n",
       "      <td>30.56</td>\n",
       "      <td>3.1</td>\n",
       "      <td>22</td>\n",
       "      <td>21.30</td>\n",
       "      <td>0.97</td>\n",
       "      <td>5.89</td>\n",
       "      <td>5.10</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Q3 2016</th>\n",
       "      <td>153</td>\n",
       "      <td>963</td>\n",
       "      <td>62</td>\n",
       "      <td>36.78</td>\n",
       "      <td>31.7</td>\n",
       "      <td>30.46</td>\n",
       "      <td>4.3</td>\n",
       "      <td>24</td>\n",
       "      <td>21.45</td>\n",
       "      <td>1.20</td>\n",
       "      <td>6.10</td>\n",
       "      <td>5.43</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Q4 2016</th>\n",
       "      <td>155</td>\n",
       "      <td>1000</td>\n",
       "      <td>61</td>\n",
       "      <td>36.11</td>\n",
       "      <td>31.2</td>\n",
       "      <td>30.11</td>\n",
       "      <td>6.7</td>\n",
       "      <td>26</td>\n",
       "      <td>21.88</td>\n",
       "      <td>1.67</td>\n",
       "      <td>6.50</td>\n",
       "      <td>5.89</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Q1 2017</th>\n",
       "      <td>156</td>\n",
       "      <td>1012</td>\n",
       "      <td>66</td>\n",
       "      <td>37.07</td>\n",
       "      <td>30.0</td>\n",
       "      <td>31.00</td>\n",
       "      <td>8.1</td>\n",
       "      <td>31</td>\n",
       "      <td>22.34</td>\n",
       "      <td>2.03</td>\n",
       "      <td>6.40</td>\n",
       "      <td>6.09</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "        Price Ratios                                                        \\\n",
       "               Price                  Price to earnings ratio (P/E)          \n",
       "Company     Facebook Google Microsoft                      Facebook Google   \n",
       "Q1 2016          155    955        66                         37.10   32.0   \n",
       "Q2 2016          150    987        69                         36.98   31.3   \n",
       "Q3 2016          153    963        62                         36.78   31.7   \n",
       "Q4 2016          155   1000        61                         36.11   31.2   \n",
       "Q1 2017          156   1012        66                         37.07   30.0   \n",
       "\n",
       "                  Income Statement                                     \\\n",
       "                         Net Sales                  Net Profit          \n",
       "Company Microsoft         Facebook Google Microsoft   Facebook Google   \n",
       "Q1 2016     30.31              2.6     20     18.70       0.80   5.43   \n",
       "Q2 2016     30.56              3.1     22     21.30       0.97   5.89   \n",
       "Q3 2016     30.46              4.3     24     21.45       1.20   6.10   \n",
       "Q4 2016     30.11              6.7     26     21.88       1.67   6.50   \n",
       "Q1 2017     31.00              8.1     31     22.34       2.03   6.40   \n",
       "\n",
       "                   \n",
       "                   \n",
       "Company Microsoft  \n",
       "Q1 2016      4.56  \n",
       "Q2 2016      5.10  \n",
       "Q3 2016      5.43  \n",
       "Q4 2016      5.89  \n",
       "Q1 2017      6.09  "
      ]
     },
     "execution_count": 95,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df2 = pd.read_excel(\"stocks_3_levels.xlsx\",header=[0,1,2])\n",
    "df2"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 96,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style>\n",
       "    .dataframe thead tr:only-child th {\n",
       "        text-align: right;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: left;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th colspan=\"2\" halign=\"left\">Income Statement</th>\n",
       "      <th colspan=\"2\" halign=\"left\">Price Ratios</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th>Net Profit</th>\n",
       "      <th>Net Sales</th>\n",
       "      <th>Price</th>\n",
       "      <th>Price to earnings ratio (P/E)</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th></th>\n",
       "      <th>Company</th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th rowspan=\"3\" valign=\"top\">Q1 2016</th>\n",
       "      <th>Facebook</th>\n",
       "      <td>0.80</td>\n",
       "      <td>2.60</td>\n",
       "      <td>155</td>\n",
       "      <td>37.10</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Google</th>\n",
       "      <td>5.43</td>\n",
       "      <td>20.00</td>\n",
       "      <td>955</td>\n",
       "      <td>32.00</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Microsoft</th>\n",
       "      <td>4.56</td>\n",
       "      <td>18.70</td>\n",
       "      <td>66</td>\n",
       "      <td>30.31</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th rowspan=\"3\" valign=\"top\">Q2 2016</th>\n",
       "      <th>Facebook</th>\n",
       "      <td>0.97</td>\n",
       "      <td>3.10</td>\n",
       "      <td>150</td>\n",
       "      <td>36.98</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Google</th>\n",
       "      <td>5.89</td>\n",
       "      <td>22.00</td>\n",
       "      <td>987</td>\n",
       "      <td>31.30</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Microsoft</th>\n",
       "      <td>5.10</td>\n",
       "      <td>21.30</td>\n",
       "      <td>69</td>\n",
       "      <td>30.56</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th rowspan=\"3\" valign=\"top\">Q3 2016</th>\n",
       "      <th>Facebook</th>\n",
       "      <td>1.20</td>\n",
       "      <td>4.30</td>\n",
       "      <td>153</td>\n",
       "      <td>36.78</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Google</th>\n",
       "      <td>6.10</td>\n",
       "      <td>24.00</td>\n",
       "      <td>963</td>\n",
       "      <td>31.70</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Microsoft</th>\n",
       "      <td>5.43</td>\n",
       "      <td>21.45</td>\n",
       "      <td>62</td>\n",
       "      <td>30.46</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th rowspan=\"3\" valign=\"top\">Q4 2016</th>\n",
       "      <th>Facebook</th>\n",
       "      <td>1.67</td>\n",
       "      <td>6.70</td>\n",
       "      <td>155</td>\n",
       "      <td>36.11</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Google</th>\n",
       "      <td>6.50</td>\n",
       "      <td>26.00</td>\n",
       "      <td>1000</td>\n",
       "      <td>31.20</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Microsoft</th>\n",
       "      <td>5.89</td>\n",
       "      <td>21.88</td>\n",
       "      <td>61</td>\n",
       "      <td>30.11</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th rowspan=\"3\" valign=\"top\">Q1 2017</th>\n",
       "      <th>Facebook</th>\n",
       "      <td>2.03</td>\n",
       "      <td>8.10</td>\n",
       "      <td>156</td>\n",
       "      <td>37.07</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Google</th>\n",
       "      <td>6.40</td>\n",
       "      <td>31.00</td>\n",
       "      <td>1012</td>\n",
       "      <td>30.00</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Microsoft</th>\n",
       "      <td>6.09</td>\n",
       "      <td>22.34</td>\n",
       "      <td>66</td>\n",
       "      <td>31.00</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "                  Income Statement           Price Ratios  \\\n",
       "                        Net Profit Net Sales        Price   \n",
       "        Company                                             \n",
       "Q1 2016 Facebook              0.80      2.60          155   \n",
       "        Google                5.43     20.00          955   \n",
       "        Microsoft             4.56     18.70           66   \n",
       "Q2 2016 Facebook              0.97      3.10          150   \n",
       "        Google                5.89     22.00          987   \n",
       "        Microsoft             5.10     21.30           69   \n",
       "Q3 2016 Facebook              1.20      4.30          153   \n",
       "        Google                6.10     24.00          963   \n",
       "        Microsoft             5.43     21.45           62   \n",
       "Q4 2016 Facebook              1.67      6.70          155   \n",
       "        Google                6.50     26.00         1000   \n",
       "        Microsoft             5.89     21.88           61   \n",
       "Q1 2017 Facebook              2.03      8.10          156   \n",
       "        Google                6.40     31.00         1012   \n",
       "        Microsoft             6.09     22.34           66   \n",
       "\n",
       "                                                 \n",
       "                  Price to earnings ratio (P/E)  \n",
       "        Company                                  \n",
       "Q1 2016 Facebook                          37.10  \n",
       "        Google                            32.00  \n",
       "        Microsoft                         30.31  \n",
       "Q2 2016 Facebook                          36.98  \n",
       "        Google                            31.30  \n",
       "        Microsoft                         30.56  \n",
       "Q3 2016 Facebook                          36.78  \n",
       "        Google                            31.70  \n",
       "        Microsoft                         30.46  \n",
       "Q4 2016 Facebook                          36.11  \n",
       "        Google                            31.20  \n",
       "        Microsoft                         30.11  \n",
       "Q1 2017 Facebook                          37.07  \n",
       "        Google                            30.00  \n",
       "        Microsoft                         31.00  "
      ]
     },
     "execution_count": 96,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df2.stack()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 104,
   "metadata": {
    "collapsed": false,
    "scrolled": true
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style>\n",
       "    .dataframe thead tr:only-child th {\n",
       "        text-align: right;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: left;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th colspan=\"3\" halign=\"left\">Net Profit</th>\n",
       "      <th colspan=\"3\" halign=\"left\">Net Sales</th>\n",
       "      <th colspan=\"3\" halign=\"left\">Price</th>\n",
       "      <th colspan=\"3\" halign=\"left\">Price to earnings ratio (P/E)</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th></th>\n",
       "      <th>Company</th>\n",
       "      <th>Facebook</th>\n",
       "      <th>Google</th>\n",
       "      <th>Microsoft</th>\n",
       "      <th>Facebook</th>\n",
       "      <th>Google</th>\n",
       "      <th>Microsoft</th>\n",
       "      <th>Facebook</th>\n",
       "      <th>Google</th>\n",
       "      <th>Microsoft</th>\n",
       "      <th>Facebook</th>\n",
       "      <th>Google</th>\n",
       "      <th>Microsoft</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th rowspan=\"2\" valign=\"top\">Q1 2016</th>\n",
       "      <th>Income Statement</th>\n",
       "      <td>0.80</td>\n",
       "      <td>5.43</td>\n",
       "      <td>4.56</td>\n",
       "      <td>2.6</td>\n",
       "      <td>20.0</td>\n",
       "      <td>18.70</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Price Ratios</th>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>155.0</td>\n",
       "      <td>955.0</td>\n",
       "      <td>66.0</td>\n",
       "      <td>37.10</td>\n",
       "      <td>32.0</td>\n",
       "      <td>30.31</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th rowspan=\"2\" valign=\"top\">Q2 2016</th>\n",
       "      <th>Income Statement</th>\n",
       "      <td>0.97</td>\n",
       "      <td>5.89</td>\n",
       "      <td>5.10</td>\n",
       "      <td>3.1</td>\n",
       "      <td>22.0</td>\n",
       "      <td>21.30</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Price Ratios</th>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>150.0</td>\n",
       "      <td>987.0</td>\n",
       "      <td>69.0</td>\n",
       "      <td>36.98</td>\n",
       "      <td>31.3</td>\n",
       "      <td>30.56</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th rowspan=\"2\" valign=\"top\">Q3 2016</th>\n",
       "      <th>Income Statement</th>\n",
       "      <td>1.20</td>\n",
       "      <td>6.10</td>\n",
       "      <td>5.43</td>\n",
       "      <td>4.3</td>\n",
       "      <td>24.0</td>\n",
       "      <td>21.45</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Price Ratios</th>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>153.0</td>\n",
       "      <td>963.0</td>\n",
       "      <td>62.0</td>\n",
       "      <td>36.78</td>\n",
       "      <td>31.7</td>\n",
       "      <td>30.46</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th rowspan=\"2\" valign=\"top\">Q4 2016</th>\n",
       "      <th>Income Statement</th>\n",
       "      <td>1.67</td>\n",
       "      <td>6.50</td>\n",
       "      <td>5.89</td>\n",
       "      <td>6.7</td>\n",
       "      <td>26.0</td>\n",
       "      <td>21.88</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Price Ratios</th>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>155.0</td>\n",
       "      <td>1000.0</td>\n",
       "      <td>61.0</td>\n",
       "      <td>36.11</td>\n",
       "      <td>31.2</td>\n",
       "      <td>30.11</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th rowspan=\"2\" valign=\"top\">Q1 2017</th>\n",
       "      <th>Income Statement</th>\n",
       "      <td>2.03</td>\n",
       "      <td>6.40</td>\n",
       "      <td>6.09</td>\n",
       "      <td>8.1</td>\n",
       "      <td>31.0</td>\n",
       "      <td>22.34</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Price Ratios</th>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>156.0</td>\n",
       "      <td>1012.0</td>\n",
       "      <td>66.0</td>\n",
       "      <td>37.07</td>\n",
       "      <td>30.0</td>\n",
       "      <td>31.00</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "                         Net Profit                  Net Sales         \\\n",
       "Company                    Facebook Google Microsoft  Facebook Google   \n",
       "Q1 2016 Income Statement       0.80   5.43      4.56       2.6   20.0   \n",
       "        Price Ratios            NaN    NaN       NaN       NaN    NaN   \n",
       "Q2 2016 Income Statement       0.97   5.89      5.10       3.1   22.0   \n",
       "        Price Ratios            NaN    NaN       NaN       NaN    NaN   \n",
       "Q3 2016 Income Statement       1.20   6.10      5.43       4.3   24.0   \n",
       "        Price Ratios            NaN    NaN       NaN       NaN    NaN   \n",
       "Q4 2016 Income Statement       1.67   6.50      5.89       6.7   26.0   \n",
       "        Price Ratios            NaN    NaN       NaN       NaN    NaN   \n",
       "Q1 2017 Income Statement       2.03   6.40      6.09       8.1   31.0   \n",
       "        Price Ratios            NaN    NaN       NaN       NaN    NaN   \n",
       "\n",
       "                                      Price                    \\\n",
       "Company                  Microsoft Facebook  Google Microsoft   \n",
       "Q1 2016 Income Statement     18.70      NaN     NaN       NaN   \n",
       "        Price Ratios           NaN    155.0   955.0      66.0   \n",
       "Q2 2016 Income Statement     21.30      NaN     NaN       NaN   \n",
       "        Price Ratios           NaN    150.0   987.0      69.0   \n",
       "Q3 2016 Income Statement     21.45      NaN     NaN       NaN   \n",
       "        Price Ratios           NaN    153.0   963.0      62.0   \n",
       "Q4 2016 Income Statement     21.88      NaN     NaN       NaN   \n",
       "        Price Ratios           NaN    155.0  1000.0      61.0   \n",
       "Q1 2017 Income Statement     22.34      NaN     NaN       NaN   \n",
       "        Price Ratios           NaN    156.0  1012.0      66.0   \n",
       "\n",
       "                         Price to earnings ratio (P/E)                   \n",
       "Company                                       Facebook Google Microsoft  \n",
       "Q1 2016 Income Statement                           NaN    NaN       NaN  \n",
       "        Price Ratios                             37.10   32.0     30.31  \n",
       "Q2 2016 Income Statement                           NaN    NaN       NaN  \n",
       "        Price Ratios                             36.98   31.3     30.56  \n",
       "Q3 2016 Income Statement                           NaN    NaN       NaN  \n",
       "        Price Ratios                             36.78   31.7     30.46  \n",
       "Q4 2016 Income Statement                           NaN    NaN       NaN  \n",
       "        Price Ratios                             36.11   31.2     30.11  \n",
       "Q1 2017 Income Statement                           NaN    NaN       NaN  \n",
       "        Price Ratios                             37.07   30.0     31.00  "
      ]
     },
     "execution_count": 104,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df2.stack(level=0)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 105,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style>\n",
       "    .dataframe thead tr:only-child th {\n",
       "        text-align: right;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: left;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th colspan=\"3\" halign=\"left\">Income Statement</th>\n",
       "      <th colspan=\"3\" halign=\"left\">Price Ratios</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th></th>\n",
       "      <th>Company</th>\n",
       "      <th>Facebook</th>\n",
       "      <th>Google</th>\n",
       "      <th>Microsoft</th>\n",
       "      <th>Facebook</th>\n",
       "      <th>Google</th>\n",
       "      <th>Microsoft</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th rowspan=\"4\" valign=\"top\">Q1 2016</th>\n",
       "      <th>Net Profit</th>\n",
       "      <td>0.80</td>\n",
       "      <td>5.43</td>\n",
       "      <td>4.56</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Net Sales</th>\n",
       "      <td>2.60</td>\n",
       "      <td>20.00</td>\n",
       "      <td>18.70</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Price</th>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>155.00</td>\n",
       "      <td>955.0</td>\n",
       "      <td>66.00</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Price to earnings ratio (P/E)</th>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>37.10</td>\n",
       "      <td>32.0</td>\n",
       "      <td>30.31</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th rowspan=\"4\" valign=\"top\">Q2 2016</th>\n",
       "      <th>Net Profit</th>\n",
       "      <td>0.97</td>\n",
       "      <td>5.89</td>\n",
       "      <td>5.10</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Net Sales</th>\n",
       "      <td>3.10</td>\n",
       "      <td>22.00</td>\n",
       "      <td>21.30</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Price</th>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>150.00</td>\n",
       "      <td>987.0</td>\n",
       "      <td>69.00</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Price to earnings ratio (P/E)</th>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>36.98</td>\n",
       "      <td>31.3</td>\n",
       "      <td>30.56</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th rowspan=\"4\" valign=\"top\">Q3 2016</th>\n",
       "      <th>Net Profit</th>\n",
       "      <td>1.20</td>\n",
       "      <td>6.10</td>\n",
       "      <td>5.43</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Net Sales</th>\n",
       "      <td>4.30</td>\n",
       "      <td>24.00</td>\n",
       "      <td>21.45</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Price</th>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>153.00</td>\n",
       "      <td>963.0</td>\n",
       "      <td>62.00</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Price to earnings ratio (P/E)</th>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>36.78</td>\n",
       "      <td>31.7</td>\n",
       "      <td>30.46</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th rowspan=\"4\" valign=\"top\">Q4 2016</th>\n",
       "      <th>Net Profit</th>\n",
       "      <td>1.67</td>\n",
       "      <td>6.50</td>\n",
       "      <td>5.89</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Net Sales</th>\n",
       "      <td>6.70</td>\n",
       "      <td>26.00</td>\n",
       "      <td>21.88</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Price</th>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>155.00</td>\n",
       "      <td>1000.0</td>\n",
       "      <td>61.00</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Price to earnings ratio (P/E)</th>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>36.11</td>\n",
       "      <td>31.2</td>\n",
       "      <td>30.11</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th rowspan=\"4\" valign=\"top\">Q1 2017</th>\n",
       "      <th>Net Profit</th>\n",
       "      <td>2.03</td>\n",
       "      <td>6.40</td>\n",
       "      <td>6.09</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Net Sales</th>\n",
       "      <td>8.10</td>\n",
       "      <td>31.00</td>\n",
       "      <td>22.34</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Price</th>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>156.00</td>\n",
       "      <td>1012.0</td>\n",
       "      <td>66.00</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Price to earnings ratio (P/E)</th>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>37.07</td>\n",
       "      <td>30.0</td>\n",
       "      <td>31.00</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "                                      Income Statement                   \\\n",
       "Company                                       Facebook Google Microsoft   \n",
       "Q1 2016 Net Profit                                0.80   5.43      4.56   \n",
       "        Net Sales                                 2.60  20.00     18.70   \n",
       "        Price                                      NaN    NaN       NaN   \n",
       "        Price to earnings ratio (P/E)              NaN    NaN       NaN   \n",
       "Q2 2016 Net Profit                                0.97   5.89      5.10   \n",
       "        Net Sales                                 3.10  22.00     21.30   \n",
       "        Price                                      NaN    NaN       NaN   \n",
       "        Price to earnings ratio (P/E)              NaN    NaN       NaN   \n",
       "Q3 2016 Net Profit                                1.20   6.10      5.43   \n",
       "        Net Sales                                 4.30  24.00     21.45   \n",
       "        Price                                      NaN    NaN       NaN   \n",
       "        Price to earnings ratio (P/E)              NaN    NaN       NaN   \n",
       "Q4 2016 Net Profit                                1.67   6.50      5.89   \n",
       "        Net Sales                                 6.70  26.00     21.88   \n",
       "        Price                                      NaN    NaN       NaN   \n",
       "        Price to earnings ratio (P/E)              NaN    NaN       NaN   \n",
       "Q1 2017 Net Profit                                2.03   6.40      6.09   \n",
       "        Net Sales                                 8.10  31.00     22.34   \n",
       "        Price                                      NaN    NaN       NaN   \n",
       "        Price to earnings ratio (P/E)              NaN    NaN       NaN   \n",
       "\n",
       "                                      Price Ratios                    \n",
       "Company                                   Facebook  Google Microsoft  \n",
       "Q1 2016 Net Profit                             NaN     NaN       NaN  \n",
       "        Net Sales                              NaN     NaN       NaN  \n",
       "        Price                               155.00   955.0     66.00  \n",
       "        Price to earnings ratio (P/E)        37.10    32.0     30.31  \n",
       "Q2 2016 Net Profit                             NaN     NaN       NaN  \n",
       "        Net Sales                              NaN     NaN       NaN  \n",
       "        Price                               150.00   987.0     69.00  \n",
       "        Price to earnings ratio (P/E)        36.98    31.3     30.56  \n",
       "Q3 2016 Net Profit                             NaN     NaN       NaN  \n",
       "        Net Sales                              NaN     NaN       NaN  \n",
       "        Price                               153.00   963.0     62.00  \n",
       "        Price to earnings ratio (P/E)        36.78    31.7     30.46  \n",
       "Q4 2016 Net Profit                             NaN     NaN       NaN  \n",
       "        Net Sales                              NaN     NaN       NaN  \n",
       "        Price                               155.00  1000.0     61.00  \n",
       "        Price to earnings ratio (P/E)        36.11    31.2     30.11  \n",
       "Q1 2017 Net Profit                             NaN     NaN       NaN  \n",
       "        Net Sales                              NaN     NaN       NaN  \n",
       "        Price                               156.00  1012.0     66.00  \n",
       "        Price to earnings ratio (P/E)        37.07    30.0     31.00  "
      ]
     },
     "execution_count": 105,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df2.stack(level=1)"
   ]
  }
 ],
 "metadata": {
  "anaconda-cloud": {},
  "kernelspec": {
   "display_name": "Python [conda root]",
   "language": "python",
   "name": "conda-root-py"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.5.2"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 1
}
